﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Splicer.Renderer;
using Splicer.Timeline;

namespace ArchiveLink
{
    internal class MockVideo
    {
        private readonly string _disk;
        private readonly string _outputFile;

        public static FileInfo GetMockFile(string volumeName, Configuration config)
        {
            var offlineFile = new FileInfo(Path.Combine(config.GetOfflineDir(), ".Mock", volumeName + ".wmv"));
            if (!offlineFile.Exists)
            {
                if (!offlineFile.Directory.Exists)
                {
                    offlineFile.Directory.Create();
                }

                var mockVideo = new MockVideo(volumeName, offlineFile.FullName);
                mockVideo.RenderOfflineMessage();

                offlineFile.Refresh();
            }
            return offlineFile;
        }

        private MockVideo(string disk, string outputFile)
        {
            _disk = disk;
            _outputFile = outputFile;
        }


        private void RenderOfflineMessage()
        {
            var bg = new SolidBrush(Color.Black);
            var fg = new SolidBrush(Color.White);
            var width = 1280;
            var height = 720;
            var text = "Please insert disk " + _disk;
            var font = new Font(System.Drawing.FontFamily.GenericSansSerif, 30);
            var duration = 10;

            Bitmap bitmap = new Bitmap(width, height);
            var graf = Graphics.FromImage(bitmap);
            graf.FillRectangle(bg, 0, 0, width, height);
            var fSize = graf.MeasureString(text, font);
            graf.DrawString(text, font, fg, (width - fSize.Width) / 2, (height - fSize.Height) / 2);


            using (ITimeline timeline = new DefaultTimeline())
            {
                IGroup group = timeline.AddVideoGroup(32, width, height);

                ITrack videoTrack = group.AddTrack();
                IClip clip1 = videoTrack.AddImage(bitmap, 0, duration); // play first image for a little while

                // fade out and back in
                //group.AddTransition(0, 2, StandardTransitions.CreateFade(), false);
                //group.AddTransition(duration - 2, duration, StandardTransitions.CreateFade(), true);

                ITrack audioTrack = timeline.AddAudioGroup().AddTrack();
                //IClip audio = audioTrack.AddAudio("testinput.wav", 0, videoTrack.Duration);
                // create an audio envelope effect, this will:
                // fade the audio from 0% to 100% in 1 second.
                // play at full volume until 1 second before the end of the track
                // fade back out to 0% volume
                //audioTrack.AddEffect(0, audio.Duration, StandardEffects.CreateAudioEnvelope(1.0, 1.0, 1.0, audio.Duration));

                // render our slideshow out to a windows media file
                var s = Assembly.GetExecutingAssembly().GetManifestResourceStream("ArchiveLink.WMV-HD-1280x720.prx");
                var tr = new StreamReader(s);
                var profile = tr.ReadToEnd();

                IRenderer renderer = new WindowsMediaRenderer(timeline, _outputFile, profile);
                renderer.Render();
            }
        }

    }
}
